#include "Program.h"
#include "entity/EntityLight.h"
#include "entity/EntityCamera.h"
#include "entity/EntitySceneNode.h"
#include "utility/Common.h"
#include <QDir>

QString Program::MediaDirectory = "media";
QString Program::WorkingSpace = "";
Configure Program::Config;
EntityLight Program::LightParam;
EntityCamera Program::CameraParam;
QMap<QString, std::shared_ptr<EntitySceneNode>> Program::SceneNodeList;
QList<QPair<QString, QString>> Program::UserScript;
QString Program::LineTrack = "";
MainWindow* Program::MainWin = nullptr;

/**
 * @brief 加载项目
 */
void Program::SetupProject()
{
    AvatarLibrary::avatar_command("engine.reset()");
    AvatarLibrary::avatar_command(Common::ReadAllText(MediaDirectory, "avatar.lua"));
    AvatarLibrary::avatar_command(QString("engine.directory('%1')").arg(QFileInfo(WorkingSpace).absolutePath().replace('\\', '/')));
    AvatarLibrary::avatar_command(LightParam.Script());
    AvatarLibrary::avatar_command(CameraParam.Script());
    AvatarLibrary::avatar_command(QString("graphics.background(%1,1.0)").arg(Config.Background.ToString()));
    AvatarLibrary::avatar_command(QString("physics.gravity(%1)").arg(Config.Gravity.ToString()));
    AvatarLibrary::avatar_command(QString("engine.input('speed',%1,%2)").arg(Config.InputSpeed[0]).arg(Config.InputSpeed[1]));
    MainWin->setSceneTree();
    for (qsizetype i = 0; i < UserScript.size(); i++) AvatarLibrary::avatar_command(UserScript[i].second);
    MainWin->showProject(WorkingSpace);
}

/**
 * @brief 加载场景
 * @param scene
 */
void Program::LoadScene(const QString &scene)
{
    WorkingSpace = "";
    SceneNodeList.clear();
    UserScript.clear();
    MainWin->setSceneTree();
    MainWin->startScene(scene);
    MainWin->showProject(scene);
}
